package com.pmcc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYMMDD = "yy.MM.dd";
	public static final String HH = "HH";
	public static final String HH_MM = "HH:mm";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM_SS_MS = "yyyy-MM-dd HH:mm:ss.ms";
	public static final String INLUXDB_TIME_TYPEX_SSSX= "yyyy-MM-dd'T'HH:mm:ss.SSSX";
	public static final String INLUXDB_TIME_TYPEX_SSSZ= "yyyy-MM-dd'T'HH:mm:ss.SS'Z'";//本地时间格式化为influxdb时间使用
	public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public final static int YEAR = 1;
	public final static int MONTH = 2;

	public static SimpleDateFormat getDateFormat(String parttern)
			throws RuntimeException {
		return new SimpleDateFormat(parttern);
	}

	public static String getDate(String time){
		Date t= DateUtil.StringToDate(time, DateUtil.INLUXDB_TIME_TYPEX_SSSX);
		return DateUtil.DateToString(t,DateUtil.YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 时间转指定格式字符串
	 * 
	 * @param date
	 * @param parttern
	 * @return
	 */
	public static String DateToString(Date date, String parttern) {
		String formatDate = null;
		if (date != null) {
			try {
				if(parttern == null || "".equals(parttern)){
					parttern = YYYY_MM_DD_HH_MM_SS;
				}
				formatDate = getDateFormat(parttern).format(date);
			} catch (Exception e) {
				formatDate = new String();
			}
		}
		return formatDate;
	}


	/**
	 * influxdb 插入时间使用
	 * timeZone  转换时间格式的所属时区
	 * 将获得时间加8小时 放入时序数据库 utc时间转换为本地时间
	 *
	 * @param time
	 * @return
	 */
	public static Date getInfluxdbTimeType(String time, String timeZone) {
		Date formatDate = null;
		if (time != null) {
			SimpleDateFormat df = new SimpleDateFormat(INLUXDB_TIME_TYPEX_SSSZ);
			df.setTimeZone(TimeZone.getTimeZone(timeZone));
			try {
				formatDate = df.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
		return formatDate;

	}
	/**
	 * 带毫秒时间 转long influxdb入库使用,时间添加了8个小时
	 * @param date
	 * @return
	 */
	public static Long getInfluxdbLongTime(String date) {
		Date time = StringToDate(date,YYYY_MM_DD_HH_MM_SS_SSS);
		Date atime=addHour(time,8);
		return atime.getTime();
	}
	/**
	 * 字符串转指定格式时间
	 * 
	 * @param date
	 * @param parttern
	 * @return
	 */
	public static Date StringToDate(String date, String parttern) {
		Date formatDate = null;
		if (date != null) {
			try {
				if(parttern == null || "".equals(parttern)){
					parttern = YYYY_MM_DD_HH_MM_SS;
				}
				formatDate = getDateFormat(parttern).parse(date);
			} catch (Exception e) {
				formatDate = new Date();
			}
		}
		return new Date(formatDate.getTime());
	}

	/**
	 * 获取一月的开始和结束日期
	 * 
	 * @param time
	 * @return
	 */
	public static Date[] getBetweenMonthTime(Date time) {
		Date[] times = new Date[2];
		times[0] = getMonthStartTime(time);
		times[1] = getMonthEndTime(time);
		return times;
	}

	/**
	 * 获取一天的开始和结束时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date[] getBetweenDayTime(Date time) {
		Date[] times = new Date[2];
		times[0] = getStartDayTime(time);
		times[1] = getEndDayTime(time);
		return times;
	}

	/**
	 * 获取一年的开始和结束时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date[] getBetweenYearTime(Date time) {
		Date[] times = new Date[2];
		times[0] = getYearStartTime(time);
		times[1] = getYearEndTime(time);
		return times;
	}

	/**
	 * 获取某天的开始时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getStartDayTime(Date time) {
		// TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return new Date(c.getTime().getTime());
	}

	/**
	 * 获取某天的结束时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getEndDayTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return new Date(c.getTime().getTime());
	}

	/**
	 * 获取某月的开始时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getMonthStartTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.DATE, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取某月的结束时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getMonthEndTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.DATE, 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DAY_OF_MONTH, -1);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取某年的开始时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getYearStartTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.DATE, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取某年的结束时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getYearEndTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.MONTH, 11);
		c.set(Calendar.DATE, 31);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 判断日期是否为空
	 * @param time
	 * @return
     */
	private static Date isNullDate(Date time) {
		if (time == null) {
			return getCurrentTime();
		}
		return time;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return Date
	 */
	public static Date getCurrentTime() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}
	/**
	 * 获取当前时间
	 *
	 * @return yyyy-MM-dd
	 */
	public static String getCurrentTimeString() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return DateToString(c.getTime(), YYYY_MM_DD);
	}

	/**
	 * 获取时间
	 * 
	 * @param year
	 *            数字
	 * @param month
	 *            数字
	 * @return
	 */
	public static Date getTime(int year, int month) {
		Calendar c = Calendar.getInstance();

		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	public static Date getTime(int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		if (year != 0) {
			c.set(Calendar.YEAR, year);
		}
		if (month != 0) {
			c.set(Calendar.MONTH, month - 1);
		}
		if (day != 0) {
			c.set(Calendar.DATE, day);
		}
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取时间
	 * 
	 * @param time
	 * @return 2014-04-1 00:00:00
	 */
	public static Date getYMTime(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.set(Calendar.DATE, 0);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取时间
	 *
	 * @param time
	 * @return 2014-04-5 00:00:00
	 */
	public static Date getYMD(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取某个时间所在月的天数
	 * 
	 * @param time
	 * @return
	 */
	public static int getNumberOfDay(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getActualMaximum(Calendar.DATE);
	}

	/**
	 * 获取年
	 * 
	 * @param time
	 * @return
	 */
	public static int getYear(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		return c.get(Calendar.YEAR);
	}

	/**
	 * 获取月
	 * 
	 * @param time
	 * @return
	 */
	public static int getMonth(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		return (c.get(Calendar.MONTH) + 1);
	}

	/**
	 * 获取天
	 * @param time
	 * @return
	 */
	public static int getDay(Date time){
		Calendar c = Calendar.getInstance();
		c.setTime(isNullDate(time));
		return c.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 比较两个时间的大小
	 * 只计算跨月的
	 * @param oldDate
	 * @param newDate
	 * @return
	 */
	public static int compareYM(Date oldDate, Date newDate) {
		long res = getYMTime(newDate).getTime() - getYMTime(oldDate).getTime();
		if (res > 0) {
			// 比之前日期大
			return 1;
		} else if (res == 0) {
			// 与之前日期相等
			return 0;
		} else {
			// 与之前日期小
			return -1;
		}

	}

	/**
	 * 比较月份、天数
	 * @param date
	 * @param now
     * @return
     */
	public static int compareMD(Date date, Date now) {

		Calendar dateC = Calendar.getInstance();
		dateC.setTime(date);
		int dateM = dateC.get(Calendar.MONTH);
		int dateD = dateC.get(Calendar.DAY_OF_MONTH);

		Calendar nowC = Calendar.getInstance();
		nowC.setTime(now);
		int nowM = nowC.get(Calendar.MONTH);
		int nowD = nowC.get(Calendar.DAY_OF_MONTH);

		if (dateM == nowM && dateD == nowD) {
			// 月份和天数相同
			return 1;
		} else {
			// 月份和天数不同
			return -1;
		}
	}

	public static int compareDate(Date date, Date now) {
		long res = now.getTime() - date.getTime();
		if (res > 0) {
			// 比之前日期大
			return 1;
		} else if (res == 0) {
			// 与之前日期相等
			return 0;
		} else {
			// 与之前日期小
			return -1;
		}

	}
	
	public static int compareTime(Date oldDate, Date newDate) {
		long res = newDate.getTime() - oldDate.getTime();
		if (res > 0) {
			// 比之前日期大
			return 1;
		} else if (res == 0) {
			// 与之前日期相等
			return 0;
		} else {
			// 与之前日期小
			return -1;
		}
	}

	/**
	 * 增加天数
	 * 
	 * @param time
	 *            时间
	 * @param days
	 *            天数
	 * @return
	 */
	public static Date addDay(Date time, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.DATE, days);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);

	}
	public static Date reduceDay(Date time, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.set(Calendar.DATE, c.get(Calendar.DATE) - days);
		return StringToDate(DateToString(new Date(c.getTime().getTime()), YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);
	}


	public static Date addHour(Date time, int hours) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.HOUR, hours);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);

	}
	/**
	 * 增加月数
	 * 
	 * @param time
	 *            时间
	 * @param monthes
	 *            月
	 * @return
	 */
	public static Date addMonth(Date time, int monthes) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.MONTH, monthes);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);

	}
	

	/**
	 * 计算天数
	 *
	 * @return  
	 */
	public static int betweenOfDay(Date time1, Date time2) {
//		Calendar to1 = new GregorianCalendar();
//		to1.setTime(time1);
//		Calendar to2 = new GregorianCalendar();
//		to2.setTime(time2);
//		int res = to2.get(Calendar.DAY_OF_YEAR) - to1.get(Calendar.DAY_OF_YEAR);
//		if (res < 0) {
//			res = (res * -1);
//		}
//		return res;

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(time1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(time2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		int res=0;
		if(year1 != year2)   //同一年
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{
				if(i%4==0 && i%100!=0 || i%400==0)    //闰年
				{
					timeDistance += 366;
				}
				else    //不是闰年
				{
					timeDistance += 365;
				}
			}

			 res = timeDistance + (day2-day1) ;
		}
		else    //不同年
		{

			 res = day2-day1;
		}
		if (res < 0) {
			res = (res * -1);
		}
		return res;
	}

	/**
	 * 计算小时数
	 *
	 * @return
	 */
	public static int betweenOfHour(Date start, Date end) {
		Calendar to1 = new GregorianCalendar();
		to1.setTime(start);
		Calendar to2 = new GregorianCalendar();
		to2.setTime(end);
		int res = to2.get(Calendar.HOUR_OF_DAY) - to1.get(Calendar.HOUR_OF_DAY);
		if (res < 0) {
			res = (res * -1);
		}
		return res;
	}
	public static int betweenOfMinute(Date start, Date end) {
		Long time=end.getTime()-start.getTime();
		Long minute=time/60000L;
		return minute.intValue();
	}

	public static Date getYesDate() {
		Date currentTime = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currentTime);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		currentTime = calendar.getTime();
		return currentTime;
	}

	public static boolean weekend(Date date){

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date == null ? new Date() : date);
		int i = calendar.get(Calendar.DAY_OF_WEEK);
		if(i == Calendar.SATURDAY || i == Calendar.SUNDAY){
			return true;
		}
		return false;
	}

	public static boolean weekend(String str){

		Date date = new Date();
		if(str != null && !"".equals(str)){
			date = StringToDate(str, YYYY_MM_DD);
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int i = calendar.get(Calendar.DAY_OF_WEEK);
		if(i == Calendar.SATURDAY || i == Calendar.SUNDAY){
			return true;
		}
		return false;
	}

	public static String add(Date date, int days){

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(year, month, day, 0, 0, 0);

		final long OFFSET_MILLIS = days * 24 * 60 * 60 * 1000L;
		Date d = new Date(calendar.getTime().getTime() + OFFSET_MILLIS);

		return DateToString(d, YYYY_MM_DD_HH_MM_SS);
	}

	public static String addReturnYMD(Date date, int days){

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(year, month, day, 0, 0, 0);

		final long OFFSET_MILLIS = days * 24 * 60 * 60 * 1000L;
		Date d = new Date(calendar.getTime().getTime() + OFFSET_MILLIS);

		return DateToString(d, YYYY_MM_DD);
	}

	/**
	 * 增加秒数
	 * @param time
	 * @param seconds
	 * @return
	 */
	public static Date addSeconds(Date time, int seconds) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.SECOND, seconds);
		return StringToDate(
				DateToString(new Date(c.getTime().getTime()),
						YYYY_MM_DD_HH_MM_SS), YYYY_MM_DD_HH_MM_SS);

	}

	/**
	 * 获取两个日期之间的所有月份
	 * @param minDate
	 * @param maxDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(sdf.parse(minDate));
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

		max.setTime(sdf.parse(maxDate));
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}
		return result;
	}


	/**
	 * 获取时间戳
	 * 时间戳从1970年1月1日00:00:00至今的秒数
	 */
	public static long getTimeStamp() {
		Date d = new Date();
		long timeStamp = d.getTime() / 1000;     //getTime()得到的是微秒， 需要换算成秒
		return timeStamp;
	}

	public static void main(String[] args) {
			Date date1=new Date();
			Date date2=StringToDate("2019-06-09 00:00:00",YYYY_MM_DD_HH_MM_SS);
			int i=compareTime(date1,date2);
		System.out.println(i);

	}

}